package com.zkyc.framework.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.constant.Constant;
import com.zkyc.framework.common.mapper.SystemLogMapper;
import com.zkyc.framework.common.pojo.Role;
import com.zkyc.framework.common.pojo.SystemLog;
import com.zkyc.framework.common.pojo.User;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.user.Util.TokenUserUtil;
import com.zkyc.framework.common.mapper.RoleMapper;
import com.zkyc.framework.common.mapper.UserMapper;
import com.zkyc.framework.user.service.LogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bing.bai
 * @create 2022/5/26
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private SystemLogMapper systemLogMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public ResultVO getLoginLogs(String beginDate, String endDate, String userName, Integer current, Integer size) {
        String currentUserId = TokenUserUtil.getCurrentUserId();
        User user = userMapper.selectById(currentUserId);
        //超级管理员能看到所有
        if (Constant.SUPERADMINUSERID.equals(user.getId()) && StringUtils.isBlank(userName)) {
            return LoginLogs(beginDate, endDate, null, LogType.登入.name(), null, current, size);
        } else {
            //非超管只能看到自己
            return LoginLogs(beginDate, endDate, user.getUsername(), LogType.登入.name(), null, current, size);
        }
    }

    @Override
    public ResultVO getOperateLogs(Integer websiteId, String beginDate, String endDate, String userName, String type, String detail, Integer current, Integer size) {
        String currentUserId = TokenUserUtil.getCurrentUserId();
        User user = userMapper.selectById(currentUserId);
        List<Role> roles = roleMapper.queryRoleByUserIdWebsiteId(currentUserId, websiteId);
        Integer isAdmin = 0;
        if (Constant.SUPERADMINUSERID.equals(user.getId())) {
            isAdmin = 2;
        }
        for (int i = 0; i < roles.size(); i++) {
            if (roles.get(i).getRoleType() == 0) {
                isAdmin = 1;
            }
        }
        if (isAdmin > 0) {
            return operateLogs(websiteId, beginDate, endDate, userName, type, detail, current, size);
        } else {
            return operateLogs(websiteId, beginDate, endDate, user.getUsername(), type, detail, current, size);
        }

    }


    private ResultVO LoginLogs(String beginDate, String endDate, String userName, String type, String detail, Integer current, Integer size) {
        Page page = new Page(current, size);
        QueryWrapper<SystemLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("operate_type", type);
        queryWrapper.orderByDesc("operate_date");
        if (StringUtils.isNotBlank(userName)) {
            queryWrapper.like("operator_username", userName);
        }
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.like("operate_type", type);
        }
        if (StringUtils.isNotBlank(detail)) {
            queryWrapper.like("operate_detail", detail);
        }
        if (StringUtils.isNotBlank(beginDate)) {
            queryWrapper.ge("operate_date", beginDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            queryWrapper.le("operate_date", endDate);
        }
        IPage result = systemLogMapper.selectPage(page, queryWrapper);
        return ResultVO.data(result);
    }

    private ResultVO operateLogs(Integer websiteId, String beginDate, String endDate, String userName, String type, String detail, Integer current, Integer size) {
        Page page = new Page(current, size);
        QueryWrapper<SystemLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("website_id", websiteId);
        queryWrapper.ne("operate_type", "登入");
        queryWrapper.orderByDesc("operate_date");
        if (StringUtils.isNotBlank(userName)) {
            queryWrapper.like("operator_username", userName);
        }
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.like("operate_type", type);
        }
        if (StringUtils.isNotBlank(detail)) {
            queryWrapper.like("operate_detail", detail);
        }
        if (StringUtils.isNotBlank(beginDate)) {
            queryWrapper.ge("operate_date", beginDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            queryWrapper.le("operate_date", endDate);
        }
        IPage result = systemLogMapper.selectPage(page, queryWrapper);
        return ResultVO.data(result);
    }


}
